<?php
namespace api\modules\sms\controllers;
use yii\web\Controller;
use api\modules\sms\services\SmsApiService;
use api\helpers\SmsHelper;

/**
 * Class ApiController
 * @package api\modules\sms\controllers
 */
class ApiController extends Controller
{
    public function actionIndex($n=1){
        $mobile = \yii::$app->getRequest()->get('mobile');
        $content = \yii::$app->getRequest()->get('content');
        $data = array(
            'mobile'=>$mobile,
            'content'=>$content
        );
        $startTime = microtime(true);
        $retList = array();
        for ($i=0;$i<$n;$i++) {
            $retList[] = $this->sendMsg($data);
            usleep(2000);
        }
        $endTime = microtime(true);
        echo $endTime-$startTime;
        SmsHelper::pr($retList);
    }
    //发送短信的接口
    private function sendMsg(array $data){
        $codSmsApiRate = $this->module->params['codSmsApiRate'];
        $SmsApiServiceObj = new SmsApiService($codSmsApiRate);
        $orderId = rand(1000,9000);
        $mobile = isset($data['mobile']) ? $data['mobile'] : '00966'.rand(520000,590000);
        $code = isset($data['content']) ? $data['content'] : rand(520000,590000);
        $data2 = $SmsApiServiceObj->getSmsApiData('ar',$orderId,$mobile,$code);
        $ret = $SmsApiServiceObj->sendSms($data2);
        return $ret;
    }

    //批量发送短信
    private function batchSendMsg(){

    }

}


